﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NFinal
{
    public class Module:NFinal.BaseModule
    {
        protected override void Rewrite(string requestedPath, HttpApplication app)
        {
            if (!System.IO.File.Exists(app.Context.Request.PhysicalPath))
            {
                int position = requestedPath.LastIndexOf('.');
                int pointer = 0;
                string actionUrl = "";
                if (position > 0)
                {
                    bool hasGet = int.TryParse(requestedPath.Substring(position - 2, 2), out pointer);
                    if (hasGet)
                    {
                        actionUrl = requestedPath.Substring(0, pointer + 1);
                    }
                    else
                    {
                        actionUrl = requestedPath;
                    }
                }
                int find = Run(app, actionUrl, requestedPath);
                if (find==0)
                {
                    app.Context.Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
                    app.Context.Response.End();
                }
            }
        }
        public int Run(HttpApplication app, string actionUrl, string requestedPath)
        {
            int find = 0;
            BaseAction action = null;
            switch (actionUrl)
            {
                case "/Index.htm":
                    find = 1; break;
                case "/a.htm":
                    find = 2; break;
                case "/b.htm":
                    find = 3; break;
                default: find = 0; break;
            }
            if (find>0)
            {
                //加上.nf后缀
                app.Context.RewritePath(requestedPath + ".nf");
            }
            return find;
        }
        public void Router(HttpApplication app,string requestedPath)
        {
            //去掉.nf后缀
            requestedPath = requestedPath.Substring(0,requestedPath.Length-3);
            int position = requestedPath.LastIndexOf('.');
            int pointer = 0;
            string actionUrl = "";
            if (position > 0)
            {
                bool hasGet = int.TryParse(requestedPath.Substring(position - 2, 2), out pointer);
                if (hasGet)
                {
                    actionUrl = requestedPath.Substring(0, pointer + 1);
                }
                else
                {
                    actionUrl = requestedPath;
                }
            }
            //switch()
        }
    }
}